/*
 * CategoryPredictor.java
 * 
 * Created on Oct 28, 2007, 7:45:34 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package procrastinator;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 *
 * @author alex
 */
public class CategoryPredictor {
    private Map<Pattern,String> patterns;

    public CategoryPredictor(DatabaseConnection db) {
        patterns = new HashMap<Pattern, String>();
        refresh(db);
    }
    
    public void refresh(DatabaseConnection db) {
        for( Map.Entry<String,String> pair : db.getPatterns().entrySet() ) {
            addPattern(pair.getKey(), pair.getValue());
        }
    }
    
    public void addPattern(String pattern, String category) {
        patterns.put(Pattern.compile(".*" + pattern + ".*", Pattern.CASE_INSENSITIVE), category);
    }
    
    /*
    public void addDefaultPatterns() {
        addPattern("wikip", "Procrastination");
        addPattern("write", "Work");
    }
     */
    
    public String predict(String activity) {
        if( activity == null ) {
            return null;
        }
        
        for( Map.Entry<Pattern,String> e : patterns.entrySet() ) {
            if( e.getKey().matcher(activity).matches() ) {
                return e.getValue();
            }
        }
        
        return null;
    }
}
